using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Data.HumanResource;
using Auto.Components;

namespace BaseSystem._System.UserControl
{
    public partial class ucDivisionOper : System.Web.UI.UserControl
    {
        public delegate void ReloadParentPage();
        public event ReloadParentPage ReloadPage;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void View(string value)
        {
            this.btnAdd.Enabled = this.btnUpdate.Enabled = false;
            this.btnDelete.Enabled = true;
            SetTextRead(true);
            ReadDiv(value);
        }
        public void Add(string value)
        {
            this.btnUpdate.Enabled = this.btnDelete.Enabled = false;
            this.btnAdd.Enabled = true;
            string[] nodes = value.Split('|');
            this.lblParentID.Text = nodes[0];

            Array.Reverse(nodes);

            this.lblPath.Text = string.Join(".", nodes);
            SetTextRead(false);
        }
        public void Modify(string value)
        {
            this.btnUpdate.Enabled = this.btnDelete.Enabled = true;
            this.btnAdd.Enabled = false;
            SetTextRead(false);
            this.txtDivID.ReadOnly = true;
            ReadDiv(value);
        }
        public void SetTextRead(bool readOnly)
        {
            this.txtContact.ReadOnly = readOnly;
            this.txtDivName.ReadOnly = readOnly;
            this.txtEmail.ReadOnly = readOnly;
            this.txtPhone.ReadOnly = readOnly;
            this.txtShortName.ReadOnly = readOnly;
            this.txtTitle.ReadOnly = readOnly;
            this.txtDivID.ReadOnly = readOnly;
        }
        private void ReadDiv(string divID)
        {
            Division div = new Division(divID);
            this.txtContact.Text = div.Contact;
            this.txtDivName.Text = div.DivName;
            this.txtEmail.Text = div.Email;
            this.txtPhone.Text = div.Phone;
            this.txtShortName.Text = div.ShortName;
            this.txtTitle.Text = div.Title;
            this.txtDivID.Text = div.DivID;
            this.lblParentID.Text = div.ParentID;
        }
        public void ClearTextBoxValue()
        {
            SetTextRead(false);
            this.txtContact.Text = "";
            this.txtDivName.Text = "";
            this.txtEmail.Text = "";
            this.txtPhone.Text = "";
            this.txtShortName.Text = "";
            this.txtTitle.Text = "";
            this.txtDivID.Text = "";
            this.lblParentID.Text = "";
            this.lblPath.Text = "";
        }

        public void Save(bool update)
        {
            Division div = new Division(this.txtDivID.Text);
            div.Contact = this.txtContact.Text;
            div.DivName = this.txtDivName.Text;
            div.Email = this.txtEmail.Text;
            div.Phone = this.txtPhone.Text;
            div.ShortName = this.txtShortName.Text;
            div.Title = this.txtTitle.Text;
            div.DivID = this.txtDivID.Text;
            div.ParentID = this.lblParentID.Text;
            div.Path = this.lblPath.Text; 
            try
            {
                if (update)
                {
                    div.Update();
                }
                else if (!div.Exists(div.DivID))
                {
                    div.Insert();
                }
            }
            catch (Exception ex)
            {
                Jscript.AjaxAlert(this.Page, ex.Message);
            }
           
        }
        public void Delete()
        {
            try
            {
                Division div = new Division();
                div.DeleteDivision(this.txtDivID.Text);
                ClearTextBoxValue();
            }
            catch (Exception ex)
            {
                Jscript.AjaxAlert(this.Page,ex.Message);
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Save(true);
        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Save(false);
            ClearTextBoxValue();
            OnReloadPage();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
            OnReloadPage();
        }
        private void OnReloadPage()
        {
            if (ReloadPage != null)
            {
                ReloadPage();
            }
        }
    }
}